
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;



	public class deltamail {
public static String vIP1;public static String vIP2;static String vIP3; public static String vReturn;
public static String vURL, vText,vLinks, vVid_tut,vTitle, vTitleverify, vDbvid,vKeyword,vEID;
public static int xTS_Rows, xRows;public static String vResult;
public static String vError;public static String [][]xlData;
public static int xTS_Cols, xCols;
public static String[][] xTS_Data;
//public static String[][]xTS;
public static WebDriver driver; 
static long vTimes;
public static void main(String[] args) throws Exception {
	File file1=new File("C:/Users/Chitra/sele lib/zipfiles/chromedriver_win32_2.0/chromedriver.exe");
	System.setProperty("webdriver.chrome.driver", file1.getAbsolutePath());
	
	driver =new ChromeDriver();
	vURL = "http://www.itelearn.com/";
	System.out.println("opening URL "+vURL);
	String xlPath ="C:/Users/Chitra/itelearn/DeltaTC19.xls";
	XLTS_Read ( xlPath);
	String Res19_Path = "c:/Users/chitra/sele lib/xTC19_res1.xls";
	System.out.println("started reading test case ");
	
	for(int i= 0; i<xTS_Rows; i++){
		vKeyword= xTS_Data[i][3];
		vIP1=xTS_Data[i][4];
		vIP2=xTS_Data[i][5];
		vIP3=xTS_Data[i][6];
		vReturn=xTS_Data[i][7];

vResult= "Pass";
vError="No Error";
try{// try this code and catch the error if any
	if (keyword_executor().equals("fail")){
				vResult="Fail";
			vError="Error in Element";
			}	
	}
	
	catch (NoSuchElementException e){
	vResult ="Fail";
	vError ="Element not found" + e;
	}
xTS_Data[i][8]=vResult;
xTS_Data[i][9]=vError;
}

System.out.println("writing in excel");

xlrwrite_ts(Res19_Path,xTS_Data);
System.out.println("done");

}



public static String keyword_executor() throws Exception{
if (vKeyword.equals("navigate_to")){
navigate_to(vURL);
System.out.println("the URL is "+vURL);
}
if(vKeyword.equals("link_size")){
link_size(vIP1, vReturn);
}
if(vKeyword.equals("get_Text")){
get_Text(vIP1) ;
System.out.println("The Text is "+get_Text(vIP1));
}
if(vKeyword.equals("action_button"))
{
action_button(vIP1,vIP2, vTimes, vReturn);
}
/*if (vKeyword.equals("get_Title")){
get_Title(vIP1, vReturn);
}*/
if (vKeyword.equals("get_verify_Text")){
get_verify_Text(vIP1,vIP3, vReturn);

}

if (vKeyword.equals("get_chapter_content")){
get_chapter_content(vIP1, vIP2, vIP3, vReturn);
}
return "Done";
}
public  static void XLTS_Read(String sPath) throws Exception{
File myxl =new File(sPath);   
FileInputStream FIS = new FileInputStream(myxl);
HSSFWorkbook WB = new HSSFWorkbook(FIS);
HSSFSheet mySheet = WB.getSheetAt(2);
xTS_Rows=mySheet.getLastRowNum() +1;
xTS_Cols=mySheet.getRow(0).getLastCellNum();
System.out.println("Rows are "+ xTS_Rows);
System.out.println("column are "+ xTS_Cols);
xTS_Data=new String[xTS_Rows][xTS_Cols];

for(int i=0;i<xTS_Rows; i++){
HSSFRow row=mySheet.getRow(i);
for (short j=0; j<xTS_Cols; j++){
HSSFCell cell=row.getCell(j);// to read the value of each cell
String value =cellToString(cell);
xTS_Data[i][j]= value;
System.out.println(value);
System.out.println("*****************");

}
}
}

/*public  static void xlTSRead(String sPath) throws Exception {
File myxl =new File(sPath);   
FileInputStream FIS = new FileInputStream(myxl);
HSSFWorkbook WB = new HSSFWorkbook(FIS);
HSSFSheet mySheet = WB.getSheetAt(2);
xTSRows=mySheet.getLastRowNum() +1;
xTSCols=mySheet.getRow(0).getLastCellNum();
System.out.println("Rows are "+ xTSRows);
System.out.println("column are "+ xTSCols);
xTSData=new String[xTSRows][xTSCols];

for(int i=0;i<xTSRows; i++){
HSSFRow row=mySheet.getRow(i);
for (short j=0; j<xTSCols; j++){
HSSFCell cell=row.getCell(j);// to read the value of each cell
String value =cellToString(cell);
xTSData[i][j]= value;
System.out.println(value);
System.out.println("*****************");

}
}
}*/
public static String cellToString(HSSFCell cell){
int type = cell.getCellType();
Object result;
switch (type){
case HSSFCell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
result =cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result="";
break;
case HSSFCell.CELL_TYPE_FORMULA:
throw new RuntimeException("no formula");
case HSSFCell.CELL_TYPE_BOOLEAN:
result=cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
throw new RuntimeException("this cell has an error");
default:
throw new RuntimeException("we dont support this cell type:  "+type);
}
return result.toString();
}	




public static void navigate_to( String vURL){
driver.navigate().to(vURL);

}
public static String get_Text(String vEID){
return vText=driver.findElement(By.xpath(vEID)).getText();
}
/*public static String get_Title(String vEID, String vReturn) {
driver.findElement(By.xpath(vEID)).isSelected();
String Text =driver.getTitle();
return ("the page title is " +Text);

}*/
public static void time_sleep(long vTimes) throws InterruptedException{
Thread.sleep(vTimes);
}
public static String action_button(String vEID,String vEID1, long vTimes, String vText) throws InterruptedException{

WebElement menu = driver.findElement(By.xpath(vEID)); //video tutoiral
String title = menu.getText();//gettting the text or title -Video Tutorial
System.out.println("the title is: "+title);
WebElement menuOption = driver.findElement(By.xpath(vEID1));//database testing 
String courseName = menuOption.getText();
System.out.println("the selected course is: "+courseName);
time_sleep(vTimes);
Actions button = new Actions(driver);// invoking Action or MouseOver action  
button.moveToElement(menu).build().perform();// 
menuOption.click();
time_sleep(vTimes);
System.out.println("the selected course " + courseName + " homepage is displayed");
return courseName;

}
public static String get_verify_Text(String vEID, String vText, String vReturn ){
System.out.println("now started verify text");
String v_Text =driver.findElement(By.xpath(vEID)).getText();
System.out.println("The Title in  the video Tutorial home page is "+ vText);
if (v_Text.equals(vText)){
System.out.println("the title is "+ v_Text);

return "Same Title ";
}else
System.out.println("the title is "+ v_Text);
return "Different Title";


}
public static int link_size(String vEID, String vReturn ){
List<WebElement> myLink_text= driver.findElements(By.xpath(vEID));
int myLink = myLink_text.size();
System.out.println("number of links in the home page is: " +myLink);
for (int i =0; i<myLink; i++){
System.out.println("Link Text is "+ myLink_text.get(i).getText());
}
return myLink;
}
public static String get_chapter_content(String vEID1, String vEID2, String vEID3, String vText ){
String xp1=vEID1;
String xp2  =vEID2;
String xp3=vEID3;
for(int k =2; k<5;k++)
{
xp3=xp1+k+xp2;
System.out.println("the chapter content title is "+ driver.findElement(By.xpath(xp3)).getText());

}
return xp3;
}
public static void xlrwrite_ts(String xPath, String [][]xlData) throws IOException {

File outfile =new File(xPath);

HSSFWorkbook WB = new HSSFWorkbook();
HSSFSheet mySheet = WB.createSheet("TESTSTEPRESULT");
for (int myrow = 0; myrow < xTS_Rows; myrow++) {
HSSFRow row = mySheet.createRow(myrow);
for (int mycol = 0; mycol < xTS_Cols; mycol++) {
HSSFCell cell = row.createCell(mycol);
cell.setCellValue(xlData[myrow][mycol]);

}
}


FileOutputStream out = new FileOutputStream(xPath);
WB.write(out);
out.flush();
out.close();


} 

public void close_browser(){
driver.close();
}

}
